package cn.dujc.superkid.activities;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.util.Log;

import java.util.ArrayList;

import cn.dujc.core.adapter.BaseQuickAdapter;
import cn.dujc.core.ui.BaseListActivity;
import cn.dujc.core.util.LogUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.adapter.MyQuestionAdapter;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.MyAskResult;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.configs.Globals;
import cn.dujc.superkid.utils.Preferences;

public class MyQuestionActivity extends BaseListActivity {
    public static final String TAG =MyQuestionActivity.class.getName();
    private java.util.List<MyAskResult.MyAskEntity> List = new ArrayList();
    private MyQuestionAdapter myQuestionAdapter;
    private int mPage = 0;
    @Override
    public void initBasic(Bundle savedInstanceState) {
        super.initBasic(savedInstanceState);
        setTitle("我的疑问");
        getSwipeRefreshLayout().setEnabled(false);//禁止下拉刷新
    }


    @Nullable
    @Override
    protected BaseQuickAdapter initAdapter() {
        myQuestionAdapter = new MyQuestionAdapter(List);
        return myQuestionAdapter;
    }

    protected void recyclerViewOtherSetup() {
        if (mRvList != null) {
            //去掉下划线
//            mRvList.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
//            myQuestionAdapter.disableLoadMoreIfNotFullPage();//禁止加载
        }
    }

    @Override
    protected void onItemClick(int position) {

    }

    @Override
    protected void loadMore() {
        loadData();

    }



    @Override
    protected void reload() {
        showRefreshing();
        mPage = 0;
        loadData();
    }

    private void loadData() {
        if (!Preferences.isLogin(this)) {
            starter().go(LoginActivity.class);
        }
        final RegisterResult loginResult = Preferences.getInstance().getLoginResult(mActivity);
        HttpHelper.getUserCenter().memberMyAsk(QMap.build()
                .put("user_id", loginResult.getUid())
                .put("channel",  Preferences.getInstance().getChannel(mActivity).getChannel_id())
                .put("logincode", loginResult.getLogincode())
                .put("num",Globals.PAGE_SIZE)
                .put("page", mPage)
                .create()).enqueue(new ResultCallBack<Result<MyAskResult>>(mActivity) {
            @Override
            public void onSuccessCallback(int code, Result<MyAskResult> body) {
                if (mPage <= 0) List.clear();
                boolean done = false;
                if (body != null){
                    final MyAskResult data = body.getData();
                    if (data != null) {
                        final java.util.List<MyAskResult.MyAskEntity> list = data.getList();
                        if(list.size()==0){
                            myQuestionAdapter.setEmptyView(R.layout.layout_empty_img);
                        }
                        if (list != null) {
                            MyQuestionActivity.this.List.addAll(list);
                        }
                        done = mPage >= data.getPage_num();
                    }
                }

                Handler handler = new Handler();
                final boolean finalDone = done;
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        notifyDataSetChanged(finalDone);
                        mPage++;
                    }
                }, 500);

            }

            @Override
            public void onFailureCallback(int code, String msg) {
                loadFailure();
                loadDone(true,false);
                if(code==500){
                    myQuestionAdapter.setEmptyView(R.layout.layout_empty_img);
                }
            }
        });
    }
}
